package show.me.the.money.util.keyManager;

import java.net.*;
import java.util.*;
import java.security.*;

public class RandomGUID {

    public String mValueBeforeMD5 = "";
    public String mValueAfterMD5 = "";
    private static Random mRand;
    private static SecureRandom mSecureRand;

    private static String s_id = "JinCp/169.254.2.2";

    static {
        mSecureRand = new SecureRandom();
        long secureInitializer = mSecureRand.nextLong();
        mRand = new Random(secureInitializer);
        try {
            s_id = InetAddress.getLocalHost().toString();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public RandomGUID() {
        getRandomGUID(false);
    }

    public RandomGUID(boolean secure) {
        getRandomGUID(secure);
    }

    private void getRandomGUID(boolean secure) {
        MessageDigest md5 = null;
        StringBuffer sbValueBeforeMD5 = new StringBuffer();

        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Error: " + e);
        }

        try {
            long time = System.currentTimeMillis();
            long rand = 0;

            if (secure) {
                rand = mSecureRand.nextLong();
            } else {
                rand = mRand.nextLong();
            }

            sbValueBeforeMD5.append(s_id);
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(Long.toString(time));
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(Long.toString(rand));

            mValueBeforeMD5 = sbValueBeforeMD5.toString();
            md5.update(mValueBeforeMD5.getBytes());

            byte[] array = md5.digest();
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < array.length; ++j) {
                int b = array[j] & 0xFF;
                if (b < 0x10) sb.append('0');
                sb.append(Integer.toHexString(b));
            }

            mValueAfterMD5 = sb.toString();

        } catch (Exception e) {
            System.out.println("Error:" + e);
        }
    }

    public String toString() {
        String raw = mValueAfterMD5.toUpperCase();
        StringBuffer sb = new StringBuffer();
        sb.append(raw.substring(0, 8));
        sb.append("-");
        sb.append(raw.substring(8, 12));
        sb.append("-");
        sb.append(raw.substring(12, 16));
        sb.append("-");
        sb.append(raw.substring(16, 20));
        sb.append("-");
        sb.append(raw.substring(20));

        return sb.toString();
    }

    public static String getInatance(){
     RandomGUID myGUID = new RandomGUID();
     return myGUID.toString();
    }
   
    public static void main(String args[]) {
        for (int i=0; i< 1; i++) {
	      RandomGUID myGUID = new RandomGUID();
	      System.out.println("Seeding String=" + myGUID.mValueBeforeMD5);
	      System.out.println("rawGUID=" + myGUID.mValueAfterMD5);
	      System.out.println("RandomGUID=" + myGUID.toString());
	      System.out.println("GUID size=" + myGUID.toString().length());
        }
    }
}
